using System;
using System.Linq;
using System.Threading;
using Application.Interface;
using Domain.Dto;
using Domain.Interface;
using Domain.Model;
using Domain.Specification;
using Gmmy.ApplicationExtension;
using Gmmy.DomainExtension;
using Gmmy.RepositoryExtension;
using Gmmy.Utility;
using Microsoft.Extensions.Logging;
using Surging.Core.CPlatform.Ioc;
using System.Threading.Tasks;
using Domain.Dto.Role;
using Gmmy.Utility.Enums;
using Surging.Core.Caching;
using Surging.Core.CPlatform.Cache;

namespace Application.Services
{
    [ModuleName("Role")]
    public class RoleService : BaseServices<Role>, IRoleService
    {
        private readonly IRoleDomainService _domainService;
        private readonly IRoleRepository _repository;
        private readonly ILogger<RoleService> _logger;
        public RoleService(IRoleRepository repository, IRoleDomainService domainService
		,ILogger<RoleService> logger
		)
            : base(repository, logger)
        {
			_domainService = domainService;
            _repository = repository;
			_logger = logger;
        }

		/// <summary>
        /// 获取角色表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> Get(GetInput input)
        {
            return await Do(input, async result =>
            {
                result.data = await _repository.GetRoleDetail(input.ID);
            });
        }

        /// <summary>
        /// 获取角色表列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> GetList(RoleListInput input)
        {
            return await Do(input, async result =>
            {
                result.data = await _repository.GetList(input,new RoleListSpecification(input));
            });
        }

        /// <summary>
        /// 创建角色表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string unk = "";
        public async Task<CustomApiResult> Create(RoleCreateInput input)
        {
            return await Do(input, async result =>
            {
                await _domainService.Create(input);
                await base.CommitAsync();
            });
        }
        /// <summary>
        /// 更新角色表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> Update(RoleUpdateInput input)
        {
            return await Do(input, async result =>
            {
                await _domainService.Update(input);
                await base.CommitAsync();
            });
        }

        /// <summary>
        /// 逻辑删除角色表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> LogicDelete(LogicDeleteInput input)
        {
            return await Do(input, async result =>
            {
                _repository.LogicDelete(new DirectSpecification<Role>(x => input.DeletedIds.Contains(x.ID)));
                await base.CommitAsync();
            });
        }
        public async Task<CustomApiResult> GetRoleListAll(EmptyRequest input)
        {
            return await Do(input, async result =>
            {
                result.data =
                    (await _repository.GetManyAsync(new DirectSpecification<Role>(x => !x.IsDeleted))).OrderBy(x =>
                        x.Sort).Select(x=>new AllRoleDto { key = x.ID, value = x.RoleName }).ToList();
            });
        }

    }
}